//AudioMgr.ts
import { Node, AudioSource, AudioClip, resources, director } from 'cc';
import { App } from '../App';
import { AudioVol, Singleton } from '../common/Common';
import UtilTool from './UtilTool';
/**
 *  这是一个用于播放音频的单件类，可以很方便地在项目的任何地方调用。
 */
export class AudioMgr extends Singleton<AudioMgr> {
    private _audioVol: AudioVol = null;
    private _isHide: boolean = false;
    private _audioSource: AudioSource;
    private _clips: Map<string, AudioClip> = new Map<string, AudioClip>()
    constructor() {
        UtilTool.printLog('音频构造');
        
        super()
        let audioMgr = new Node();
        audioMgr.name = '__audioMgr__';
        director.getScene().addChild(audioMgr);
        //标记为常驻节点，这样场景切换的时候就不会被销毁了
        director.addPersistRootNode(audioMgr);
        //添加 AudioSource 组件，用于播放音频。
        this._audioSource = new AudioSource()
    }
    public get audioSource() {
        return this._audioSource;
    }
    /**
     *播放短音频 单次播放
     * @param sound clip or url for the audio
     * @param volume 
     */
    playOneShot(sound: AudioClip | string, music: boolean = true, volume: number = 1.0) {
        if (App.EFFECT_SW=="0") {
            return
        }

        // UtilTool.printLog(sound,music,'sound');
        
        if (sound instanceof AudioClip) {
            this._audioSource.playOneShot(sound, volume);
        }
        else {
            UtilTool.printLog(this._clips,'_clips');
            
            let audio = this._clips.get(sound);
            // if (audio && this._clips.get(sound) instanceof AudioClip) {
            //     this._audioSource.playOneShot(audio, volume);
            //     return;
            // }
            let sounds = music ? `audio/music/${sound}` : `audio/sound/${sound}`
            resources.load(sounds, (err, clip: AudioClip) => {
                if (err) {
                    UtilTool.printLog(err);
                }
                else {
                    try {
                        this._clips.set(sound, clip);
                        this._audioSource.playOneShot(clip, volume);   
                    } catch (error) {
                        UtilTool.printLog('音频播放失败');
                        
                    }
                }
            });
        }
    }

    /**
     * 播放长音频，比如 背景音乐
     * @param sound 
     * @param volume 
     */
    play(sound?: AudioClip | string, music: boolean = true, volume: number = 1.0, loop = true) {
        if (App.MUSIC_SW=="0") {
            return
        }

        UtilTool.printLog('开始播放！！！！！！！');
        
        if (sound instanceof AudioClip) {
            this._audioSource.clip = sound;
            this._audioSource.play();
            this.audioSource.volume = volume;
            this.audioSource.loop = loop;
        }if (this._audioSource && !sound) {
            //如果有背景音乐并且 sound 没传
            // 用默认的
            this._audioSource.play();
            this.audioSource.volume = volume;
            this.audioSource.loop = loop;
        } else {
            //首页背景音乐 / 游戏背景音乐
            UtilTool.printLog(sound,typeof sound , music,'播放长音乐 ===========');
            
            if (typeof sound == "string") {
                this.stop();
            }
            let sounds = `audio/music/${sound}`;
            UtilTool.printLog(sounds,'sounds');
            
            resources.load(sounds, (err, clip: AudioClip) => {
                if (err) {
                    UtilTool.printLog(err);
                }
                else {
                    this._clips.set(sound, clip)
                    // if (this._audioSource==null) {
                        this._audioSource = new AudioSource();
                    // }
                    
                    this._audioSource.clip = clip;
                    this._audioSource.play();
                    UtilTool.printLog(this._audioSource,'this._audioSource');
                    
                    this.audioSource.volume = volume
                    this.audioSource.loop = loop;
                    // setTimeout(() =>{
                    //     this._audioSource.stop();
                    // },5000)
                }
            });
        }
    }

    /**
     * 设置背景音乐音量大小
     */ 
    volume(val: number) {
         if (this._audioSource) {
            this.audioSource.volume = val
        }
    }

    /**
     * 停止播放
     */
    stop() {
        UtilTool.printLog('停止播放！！！！！');
        
        if (this._audioSource) {
            this._audioSource.stop();
        }
    }

    /**
     * 暂停播放
     */
    pause(sound: AudioClip | string) {
        if (sound instanceof AudioClip) {
            this._audioSource.clip = sound;
        } else {
            this._audioSource.clip = this._clips.get(sound)
        }
        this._audioSource.pause();
    }
    /**
     * 继续播放
     */
    resume(sound: AudioClip | string) {
        if (sound instanceof AudioClip) {
            this._audioSource.clip = sound;
        } else {
            this._audioSource.clip = this._clips.get(sound)
        }
        this._audioSource.play();
    }

    protected onDestroy() {
        this._audioSource.destroy();
        delete this._audioSource;
    }
}